package com.ck.demo.web;

import com.ck.demo.entity.Emp;
import com.ck.demo.entity.User;
import com.ck.demo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Resource
    UserService userService;

    @GetMapping("login")
    public String login(String username, String password, HttpSession session,Model model){
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            throw new RuntimeException("用户名或密码为错误");
        }
        User user = userService.queryUser(username, password);
        if(user!=null){
            //登录成功
            session.setAttribute("user",user);
            return "dashboard";
        }else{
            model.addAttribute("msg","用户名或密码错误");
            return "index";
        }
    }

    //局部异常处理
    @ExceptionHandler(value = {Exception.class})
    public String exceptionHandler(Exception e, Model model){
        model.addAttribute("error",e.getMessage());
        return "error";
    }
}
